package GUILayer;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import CtrLayer.*;
import ModelLayer.*;
import javax.swing.JPasswordField;

public class LoginGUI {

	private JFrame loginWindow;
	private JTextField userNameField;
	private static PersonCtr personCtr;
	private static int heightStartWindow;
    private static int widthStartWindow;
    private Employee employee;
    private JPasswordField passwordField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				
				try {
					LoginGUI loginGUI = new LoginGUI();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public LoginGUI() {
	
	    initialize();
	    personCtr = personCtr.getInstance();	
    }

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		loginWindow = new JFrame();
		loginWindow.setTitle("Login");
		loginWindow.setBounds(100, 100, 272, 227);
		loginWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		loginWindow.getContentPane().setLayout(null);
		loginWindow.setVisible(true);
		
		JLabel lblBrugernavn = new JLabel("Brugernavn:");
		lblBrugernavn.setBounds(10, 11, 168, 14);
		loginWindow.getContentPane().add(lblBrugernavn);
		
		JLabel lblKodeord = new JLabel("Kodeord:");
		lblKodeord.setBounds(10, 70, 83, 14);
		loginWindow.getContentPane().add(lblKodeord);
		
		userNameField = new JTextField();
		userNameField.setBounds(10, 36, 168, 20);
		loginWindow.getContentPane().add(userNameField);
		userNameField.setColumns(10);
		
		JButton btnNewButton = new JButton("Accepter");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				login();
				}
		});
		btnNewButton.setBounds(10, 126, 89, 23);
		loginWindow.getContentPane().add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Luk program");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnNewButton_1.setBounds(111, 126, 111, 23);
		loginWindow.getContentPane().add(btnNewButton_1);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(10, 95, 168, 20);
		loginWindow.getContentPane().add(passwordField);
		
		JMenuBar menuBar = new JMenuBar();
		loginWindow.setJMenuBar(menuBar);
		
		JMenu fileMenu = new JMenu("Filer");
		menuBar.add(fileMenu);
		
		JMenuItem endProgramOption = new JMenuItem("Luk program ");
		fileMenu.add(endProgramOption);
		
		JMenu helpMenu = new JMenu("Hj\u00E6lp");
		menuBar.add(helpMenu);
		
		JMenuItem aboutOption = new JMenuItem("Om");
		helpMenu.add(aboutOption);
	}
	
	private void login()
	{
		try
		{
		employee = personCtr.login(userNameField.getText(), passwordField.getText());
		if(employee != null)
		{
		
			System.out.println("OK");
			MainGUI mainGUI = new MainGUI(employee);
			//mainGUI.setBounds(heightStartWindow,widthStartWindow,1050,600);
            //mainGUI.frmHovedMenu.setVisible(true);

            userNameField.setText("");
            passwordField.setText("");
            //this.setVisible(false);
            loginWindow.setVisible(false);
		}
		
		else
		{
            //loginWindow = this;
            JOptionPane.showMessageDialog(loginWindow, "Invalid login or password", "Error Message", JOptionPane.ERROR_MESSAGE);
            
            userNameField.setText("");
            passwordField.setText("");
		}
		
	}
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            //loginWindow = this;
            JOptionPane.showMessageDialog(loginWindow, "Invalid login or password", "Error Message", JOptionPane.ERROR_MESSAGE);
            
            userNameField.setText("");
            passwordField.setText("");
        }
}
}
